﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using Formsy.Config;

namespace Formsy.Common
{
    public class FormField
    {
        #region Public Properties

        public string Id;
        public string FieldName;
        public Label Label;
        public string Type;
        public Control Control;
        public List<FieldValidator> Validators = new List<FieldValidator>();
        public string[] ValidExtensions;
        public bool IgnoreData;

        #endregion



        #region Value

        /// <summary>
        /// Returns the value of the control (eg. Control.Text if it is a TextBox, Control.SelectedValue if it is a DropDownList)
        /// </summary>
        public string Value
        {
            get
            {
                string value = string.Empty;
                // work out which type of control
                if (Type == ControlType.TextBox
                    || Type == ControlType.TextArea
                    || Type == ControlType.EmailPair
                    || Type == ControlType.PasswordField
                    || Type == ControlType.PasswordPair
                    || Type == ControlType.DatePicker)
                    value = ((TextBox)Control).Text;
                else if (Type == ControlType.CheckBox || Type == ControlType.RadioButton)
                    value = ((CheckBox)Control).Checked.ToString();
                else if (Type == ControlType.HiddenField)
                    value = ((HiddenField)Control).Value;
                else if (Type == ControlType.DropDownList)
                    value = ((DropDownList)Control).SelectedValue;
                else if (Type == ControlType.CheckBoxList)
                {
                    value = ((CheckBoxList)Control)
                                .Items
                                .Cast<ListItem>()
                                .Where(x => x.Selected == true)
                                .Select(x => x.Value)
                                .ToArray()
                                .ToCommaDelimited();
                }
                else if (Type == ControlType.RadioButtonList)
                    value = ((RadioButtonList)Control).SelectedValue;
                else
                {
                    var control = Control as TextBox;
                    if (control != null)
                    {
                        value = ((TextBox)Control).Text;
                    }

                } return value;
            }
        }
        
        #endregion



        #region public FormField(string id, string fieldName, Control control, Label label, string type, bool ignoreData)

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id">The Id of the control</param>
        /// <param name="fieldName">The "Label" attribute in the xml. eg. &lt;field Label="My Custom Field"&gt;</param>
        /// <param name="control">The control</param>
        /// <param name="label">The Label for this field. Eg. A A TextBox may have the Label "First Name".</param>
        /// <param name="type">The type of control. eg. ControlTypes.TestBox.</param>
        public FormField(string id, string fieldName, Control control, Label label, string type, bool ignoreData)
        {
            Id = id;
            FieldName = fieldName;
            Control = control;
            Label = label;
            Type = type;
            IgnoreData = ignoreData;
        }
        
        #endregion



        #region public FormField(string id, string fieldName, Control control, Label label, string type, string[] validExtensions)

        public FormField(string id, string fieldName, Control control, Label label, string type, string[] validExtensions)
        {
            Id = id;
            FieldName = fieldName;
            Control = control;
            Label = label;
            Type = type;
            ValidExtensions = validExtensions;
        }
        
        #endregion



        #region AddFieldValidator

        public void AddFieldValidator(BaseValidator val, string type)
        {
            Validators.Add(new FieldValidator(val.ID, val, type));
        }

        #endregion

    }
}
